/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import ejb.common.AccountManagerLocal;
import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.security.auth.login.AccountException;

/**
 *
 * @author Carmela
 */
public class ReimpostaPassword implements Serializable{

    @EJB
    private AccountManagerLocal accountManager;
    private String password;
    private Long idAccount;

    /**
     * Get the value of password
     *
     * @return the value of password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set the value of password
     *
     * @param password new value of password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Get the value of idAccount
     *
     * @return the value of idAccount
     */
    public Long getIdAccount() {
        return idAccount;
    }

    /**
     * Set the value of idAccount
     *
     * @param idAccount new value of idAccount
     */
    public void setIdAccount(Long idAccount) {
        this.idAccount = idAccount;
    }

    public String reimposta() {
        try {
            accountManager.modificaPassword(password, idAccount);
            return "SMP";
        } catch (AccountException ex) {
            Logger.getLogger(ReimpostaPassword.class.getName()).log(Level.SEVERE, null, ex);
            return "FMP";
        }

    }

    /**
     * Creates a new instance of ReimpostaPassword
     */
    public ReimpostaPassword() {
    }

    @PostConstruct
    public void init() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        Map<String, String> map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String ida = map.get(utility.Utility.ID_REQUEST_STRING);
        if (ida != null) {
            if (!ida.matches("[0-9]{1,}")) {
                try {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/E-Market-war/ReimpostaPassword/ErroreReimpostaPassword.jsf");
                } catch (IOException ex) {
                    Logger.getLogger(ReimpostaPassword.class.getName()).log(Level.SEVERE, null, ex);
                }
                return;
            }
            this.idAccount = Long.parseLong(ida);
        } else {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/E-Market-war/ReimpostaPassword/ErroreReimpostaPassword.jsf");
            } catch (IOException ex) {
                Logger.getLogger(ReimpostaPassword.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
